package com.xiyou.week05.binarySearch;

/**
 * @ClassName：Search
 * @Author：西柚
 * @Date：2022/1/26 9:28 下午
 * @Versiion：1.0
 */
public class Search {

    public int search(int[] nums, int target) {
        int left = 0, right = nums.length - 1;
        while (left <= right) {
            int mid = (left + right) / 2;
            if (nums[mid] == target) {
                return mid;
            }
            if (nums[mid] < target) {
                left = mid + 1;
            } else {
                right = mid - 1;
            }
        }
        return -1;
    }

    public static void main(String[] args) {
        int[] nums = new int[]{-1, 0, 3, 5, 9, 12};
        int target = 9;
        Search search = new Search();
        System.out.println(search.search(nums, target));
    }

}
